#include "vtor_i2c.h"

#ifdef __VTOR_I2C__
#include "main.h"

// 加入用户工程宏
#ifdef __DEMO_PROJECT__



extern int32_t VtorShell_Scan(uint8_t*);


// 加入需要依赖的变量
// 使用背心 接收点led当作i2c

VtorI2c VtorI2cMaster_demoArray[3] = {0};
VtorI2c* qmcVtorI2c = NULL;
VtorI2c* masterI2c = NULL;
VtorI2c* oledI2c = NULL;
VtorI2c* mpu6050VtorI2c = NULL;
VtorI2c* stc8I2c = NULL;
VtorI2c* shellI2c = NULL;

typedef struct
{
	VtorI2c* i2c;
	GPIO_TypeDef* sclPort;
	uint16_t sclPin;
	GPIO_TypeDef* sdaPort;
	uint16_t sdaPin;
}VtorI2cMaster_Stm32HalArray;

const static VtorI2cMaster_Stm32HalArray VtorI2cMasterGpioArray[] = 
{
	//GPIOB, GPIO_PIN_6, GPIOB, GPIO_PIN_7, // demo i2c slave
	{&VtorI2cMaster_demoArray[0], GPIOA, GPIO_PIN_4, GPIOA, GPIO_PIN_6},
	//GPIOB, GPIO_PIN_6, GPIOB, GPIO_PIN_7, // log i2c slave
	//GPIOB, GPIO_PIN_4, GPIOB, GPIO_PIN_5, // log i2c slave
	
	{NULL, NULL, 0, NULL, 0},
};

int8_t VtorI2cMaster_FindI2cIndex(VtorI2c* i2c)
{
	for(int8_t i = 0; VtorI2cMasterGpioArray[i].i2c != NULL; i++)
	{
		if(i2c == VtorI2cMasterGpioArray[i].i2c)
		{
			return i;
		}
	}
	return -1;
}


void VtorI2c_WriteScl(VtorI2c* i2c, uint8_t level)
{
	int8_t i = VtorI2cMaster_FindI2cIndex(i2c);
	if(i >= 0)
	{
		HAL_GPIO_WritePin(
			VtorI2cMasterGpioArray[i].sclPort,
			VtorI2cMasterGpioArray[i].sclPin,
			(GPIO_PinState)level);
	}
}

uint8_t VtorI2c_ReadScl(VtorI2c* i2c)
{
	int8_t i = VtorI2cMaster_FindI2cIndex(i2c);
	if(i >= 0)
	{
		return HAL_GPIO_ReadPin(
			VtorI2cMasterGpioArray[i].sclPort,
			VtorI2cMasterGpioArray[i].sclPin);
	}
	return 0;
}

void VtorI2c_WriteSda(VtorI2c* i2c, uint8_t level)
{
	int8_t i = VtorI2cMaster_FindI2cIndex(i2c);
	if(i >= 0)
	{
		HAL_GPIO_WritePin(
			VtorI2cMasterGpioArray[i].sdaPort,
			VtorI2cMasterGpioArray[i].sdaPin,
			(GPIO_PinState)level);
	}
}

uint8_t VtorI2c_ReadSda(VtorI2c* i2c)
{
	int8_t i = VtorI2cMaster_FindI2cIndex(i2c);
	if(i >= 0)
	{
		return HAL_GPIO_ReadPin(
			VtorI2cMasterGpioArray[i].sdaPort,
			VtorI2cMasterGpioArray[i].sdaPin);
	}
	return 0;
}

void VtorI2c_SetSdaDir(VtorI2c* i2c, uint8_t dir)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	if(dir == GPIO_DIR_OUTPUT)
	{
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
	}
	else
	{
		GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	}
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	
	// 根据不同实例进行细分配置
	int8_t i = VtorI2cMaster_FindI2cIndex(i2c);
	if(i >= 0)
	{
		GPIO_InitStruct.Pin = VtorI2cMasterGpioArray[i].sdaPin;
		HAL_GPIO_Init(VtorI2cMasterGpioArray[i].sdaPort, &GPIO_InitStruct);
	}
	
}


// 初始化IIC的IO口 给函数指针赋值
void VtorI2c_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	
	// 根据不同实例进行细分配置
	for(int8_t i = 0; VtorI2cMasterGpioArray[i].i2c != NULL; i++)
	{
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
		GPIO_InitStruct.Pin = VtorI2cMasterGpioArray[i].sclPin;
		HAL_GPIO_Init(VtorI2cMasterGpioArray[i].sclPort, &GPIO_InitStruct);
		
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
		GPIO_InitStruct.Pin = VtorI2cMasterGpioArray[i].sdaPin;
		HAL_GPIO_Init(VtorI2cMasterGpioArray[i].sdaPort, &GPIO_InitStruct);
		
		VtorI2c_Stop(VtorI2cMasterGpioArray[i].i2c);
	}
	
	qmcVtorI2c = &VtorI2cMaster_demoArray[0];
	mpu6050VtorI2c = &VtorI2cMaster_demoArray[0];
	oledI2c = &VtorI2cMaster_demoArray[0];
	shellI2c = &VtorI2cMaster_demoArray[0];

}


void VtorTimer_VtorI2cMasterBufferTask()
{

	uint8_t rxBuf[30] = {"-------"};;
	uint8_t rxBufLen = 0;
//#define __VTOR_I2C_MASTER_BUFFER_TX_TEST__
#ifdef __VTOR_I2C_MASTER_BUFFER_TX_TEST__
	static uint8_t txLen = 5;
	txLen++;
	txLen %= 10;
	if(txLen < 4)
	{
		txLen += 5;
	}
	rxBuf[txLen - 5] = txLen / 10 + '0';
	rxBuf[txLen - 4] = txLen % 10 + '0';
	rxBuf[txLen - 3] = '+';
	rxBuf[txLen - 2] = '\r';
	rxBuf[txLen - 1] = '\n';
	rxBuf[txLen] = '\0';
	VtorI2cMaster_WriteBuffer(shellI2c, BUFFER_I2C_SLAVE_ADDR, rxBuf, txLen);
#endif
	
	rxBufLen = VtorI2cMaster_ReadBuffer(shellI2c, BUFFER_I2C_SLAVE_ADDR, rxBuf, 30 - 1);
	if(rxBufLen > 0)
	{
		VtorShell_Scan(rxBuf);
	}
}


inline void VtorI2c_Delay(uint32_t tick)
{

	//HAL_Delay(1);

	for(int i = 0; i < 400; i++)
		__NOP();
	__NOP();
	__NOP();
	__NOP();
}


#endif

#endif // __VTOR_I2C__

